import 'package:flutter/material.dart';
import 'package:star_author/utils/utils.dart';
import 'package:pull_to_refresh_notification/pull_to_refresh_notification.dart';

double get maxDragOffset => 50;

enum PullToRefreshHeaderStatus { loading, stop }

class LoadingNotify extends ChangeNotifier {
  PullToRefreshHeaderStatus status = PullToRefreshHeaderStatus.stop;

  start() {
    status = PullToRefreshHeaderStatus.loading;
    notifyListeners();
  }

  stop() {
    status = PullToRefreshHeaderStatus.stop;
    notifyListeners();
  }
}

LoadingNotify loadingNotify = new LoadingNotify();

class PullToRefreshHeader extends StatelessWidget {
  late Function(double)? onDrag;

  PullToRefreshHeader({
    Key? key,
    this.onDrag,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PullToRefreshContainer((PullToRefreshScrollNotificationInfo? info) {
      if (info != null) {
        double dragOffset = info.dragOffset ?? 0.0;
        onDrag?.call(dragOffset);
        dragOffset = dragOffset >= maxDragOffset ? maxDragOffset : dragOffset;
        bool isDrag = info.mode == RefreshIndicatorMode.drag;
        bool isLoading = info.mode == RefreshIndicatorMode.refresh ||
            info.mode == RefreshIndicatorMode.snap;
        bool isDone = info.mode == RefreshIndicatorMode.done ||
            info.mode == RefreshIndicatorMode.canceled ||
            info.mode == RefreshIndicatorMode.error;
        if (isDrag && !GetTimer.isPause) {
          GetTimer.pause();
        } else if (isLoading) {
          loadingNotify.start();
        } else if (isDone) {
          GetTimer.start();
          loadingNotify.stop();
        } else {
          loadingNotify.stop();
        }
        return Container(
          height: dragOffset,
          child: Transform.rotate(
            angle: dragOffset * -0.2,
            child: _LoadingImageWidget(),
          ),
        );
      } else {
        return Container();
      }
    });
  }
}

class _LoadingImageWidget extends StatefulWidget {
  _LoadingImageWidgetState createState() => _LoadingImageWidgetState();
}

class _LoadingImageWidgetState extends State<_LoadingImageWidget>
    with TickerProviderStateMixin {
  late AnimationController _circleController;

  void _initCircleAnimationController() {
    _circleController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 1000),
    );
  }

  void handleNotify() {
    if (loadingNotify.status == PullToRefreshHeaderStatus.loading) {
      _circleController.repeat();
    } else if (loadingNotify.status == PullToRefreshHeaderStatus.stop) {
      _circleController.stop();
    }
  }

  @override
  void initState() {
    _initCircleAnimationController();
    super.initState();
    loadingNotify.addListener(handleNotify);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    loadingNotify.removeListener(handleNotify);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _circleController,
      builder: (__ctx, __child) {
        return Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Transform.rotate(
              angle: 25 * _circleController.value,
              child: Padding(
                padding: GetPadding.only(top: 12, bottom: 12),
                child: Image.asset(
                  'images/lt_common/logo_circle.png',
                  fit: BoxFit.contain,
                ),
              ),
            )
          ],
        );
      },
    );
  }
}
